package com.web.music.portal.mvc;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.web.music.interfaces.ArtistService;
import com.web.music.interfaces.PersonService;
import com.web.music.model.Person;
import com.web.music.model.Artist;

public class DeletePersonController extends DeleteController {
	
	private ArtistService artistService;
	private PersonService personService;

	private boolean existsPersonArtists(Person person){
		List<Artist> artists = artistService.findAll();
		for (Artist a : artists) {
			if (a.existsArtistPerson(person)) {
				return true;
			}
		}
		return false;	
	}
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Long id = Long.parseLong(request.getParameter("id"));
		Person person = personService.getById(id);

		if (existsPersonArtists(person)) {
			ModelAndView result = new ModelAndView("delete_errors");		
			return result;
		}
		return super.handleRequestInternal(request, response);
	}

	public ArtistService getArtistService() {
		return artistService;
	}

	public void setArtistService(ArtistService artistService) {
		this.artistService = artistService;
	}

	public PersonService getPersonService() {
		return personService;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}
	
}
